package com.speedchina.common.message.wechat.message;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Markdown 类型
 * @author zyw
 */
public class MarkdownMessage implements Message {

    private List<String> items = new ArrayList<String>();

    public void add(String text) {
        items.add(text);
    }

    /**
     * 图片（群机器人markdown消息不支持显示图片语法的，如需要发送图片可以使用图文类型这些发送）
     * @param imageUrl
     * @return
     */
    public static String getImageText(String imageUrl) {
        return "![image](" + imageUrl + ")";
    }

    /**
     * 1-6级（支持1至6级标题，注意#与文字中间要有空格）
     * @param headerType
     * @param text
     * @return
     */
    public static String getHeaderText(int headerType, String text) {
        if (headerType < 1 || headerType > 6) {
            throw new IllegalArgumentException("headerType should be in [1, 6]");
        }

        StringBuffer numbers = new StringBuffer();
        for (int i = 0; i < headerType; i++) {
            numbers.append("#");
        }
        return numbers + " " + text;
    }

    /**
     * 加粗
     * @param text
     * @return
     */
    public static String getBoldText(String text) {
        return "**" + text + "**";
    }

    /**
     * 链接
     * @param text
     * @return
     */
    public static String getLinkText(String text, String href) {

        return "[" + text + "](" + href + ")";
    }


    /**
     * > 引用文字
     * @param text
     * @return
     */
    public static String getReferenceText(String text) {
        return "> " + text;
    }
    public static  String getTestColor(String text,String color){
        String msg = null;
        if("info".equals(color)){
            msg =  "<font color=\"info\">"+text+"</font>";
        }else if("comment".equals(color)){
            msg=  "<font color=\"comment\">"+text+"</font>";
        }else if("warning".equals(color)){
            msg =  "<font color=\"warning\">"+text+"</font>";
        }
        return msg;
    }
    public String toJsonString() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("msgtype", "markdown");

        Map<String, Object> markdown = new HashMap<String, Object>();

        StringBuffer markdownText = new StringBuffer();
        for (String item : items) {
            markdownText.append(item + "\n");
        }
        markdown.put("content", markdownText.toString());
        result.put("markdown", markdown);

        return JSON.toJSONString(result);
    }
}
